package com.gcb.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcb.common.constant.CommonConstants;
import com.gcb.common.enums.AuditSourceEnum;
import com.gcb.common.enums.AuthStatusEnum;
import com.gcb.common.enums.GcbUserEnum;
import com.gcb.domain.dto.CompanyUserAuditQueryDTO;
import com.gcb.domain.dto.CompanyUserAuditSaveDTO;
import com.gcb.domain.entity.CompanyUser;
import com.gcb.domain.entity.CompanyUserAudit;
import com.gcb.domain.vo.CompanyUserAuditVO;
import com.gcb.mapper.CompanyUserAuditMapper;
import com.gcb.service.ICompanyUserAuditService;
import com.gcb.service.ICompanyUserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


@Service
public class CompanyUserAuditServiceImpl extends ServiceImpl<CompanyUserAuditMapper, CompanyUserAudit> implements ICompanyUserAuditService {
    @Resource
    private CompanyUserAuditMapper mapper;
    @Resource
    @Lazy
    private ICompanyUserService companyUserService;

    @Override
    public List<CompanyUserAuditVO> list(CompanyUserAuditQueryDTO dto) {
        return mapper.list(dto);
    }

    @Override
    public void editStatusBatch(CompanyUserAuditSaveDTO dto) {
        // 批量修改审核状态
        mapper.updateStatusBatch(dto);
        // 审批通过，添加企业用户
        if (AuthStatusEnum.VERIFIED.getCode().equals(dto.getAuthStatus())) {
            List<CompanyUserAudit> companyUserAudits = mapper.selectBatchIds(Arrays.asList(dto.getIds()));
            companyUserAudits.forEach(item -> {
                CompanyUser companyUser = getCompanyUser(item.getUserId(), item.getCompanyId(), item.getInviteBy());
                // 保存或更新企业用户
                companyUserService.saveOrUpdate(companyUser,
                        new LambdaQueryWrapper<CompanyUser>()
                                .eq(CompanyUser::getCompanyId, companyUser.getCompanyId())
                                .eq(CompanyUser::getUserId, companyUser.getUserId()));
            });
        }
    }

    private CompanyUser getCompanyUser(Long userId, Long companyId, Long inviteBy) {
        CompanyUser companyUser = new CompanyUser();
        companyUser.setCompanyId(companyId);
        companyUser.setUserId(userId);
        companyUser.setHireDate(LocalDateTime.now());
        companyUser.setStatus(CommonConstants.CompanyUser.STATE_NORMAL);
        companyUser.setType(GcbUserEnum.COMMON_USER.getCode());
        companyUser.setInviteBy(inviteBy);
        companyUser.setCreateBy(userId);
        companyUser.setUpdateBy(userId);
        return companyUser;
    }

    @Override
    public CompanyUserAuditVO getDetail(Long id) {
        CompanyUserAuditVO vo = mapper.getDetail(id);
        vo.setSourceStr(AuditSourceEnum.getInfo(vo.getSource()));
        vo.setAuthStatusStr(AuthStatusEnum.getInfo(vo.getAuthStatus()));
        return vo;
    }
}
